package com.pet.client.common;

import java.io.File;
import java.util.Date;
import java.util.List;

import com.pet.framework.page.ATTPage;
import com.pet.framework.page.Page;
import com.pet.framework.page.PagerFactory;
import com.pet.framework.util.BaseAction;
import com.pet.framework.util.HttpHelp;
import com.pet.server.bo.common.ArticleService;
import com.pet.server.po.Article;
import com.pet.server.po.ArticleTemp;
import com.pet.server.po.Subject;
import com.pet.system.constants.ATTSystem;
import com.pet.system.constants.Status;
import com.pet.system.util.FolderManager;

public class ArticleAction extends BaseAction {
   /**
	 * 
	 */
   private static final long serialVersionUID = 1L;
   private Article article;
   private Integer id;
   private ArticleService articleService;
   private File ico,image1,image2,image3;
   private String icoFileName,image1FileName,image2FileName,image3FileName;
   private File upload;
   private String uploadFileName;
   private Subject subject;
   public String imgUrl;
   public ArticleTemp articleTemp;
   
   /**
	 * 活动页
	 * @return
	 */
	public String toSubjectImg(){
		System.out.println("imgUrl1111-"+imgUrl);
		setAttr("imgUrl01", imgUrl);
		return SUCCESS;
	}
   
   /**
    * 删除收藏数据
    * @return
    */
   public String deleteSubject(){
	   Subject v = articleService.getObject(id, Subject.class);
	   if(null!=v){
		   v.setState(ATTSystem.DEV);
		   articleService.updateObject(v);
	   }
	   return SUCCESS;
   }
	/**
	 * 删除阅读数据
	 * @return
	 */
	public String deleteArticle(){
		Article v = articleService.getObject(id, Article.class);
		if(null!=v){
			v.setState(ATTSystem.DEV);
			articleService.updateObject(v);
		}
		return SUCCESS;
	}
   public String deleteArticleTemp(){
	   ArticleTemp v = articleService.getObject(id, ArticleTemp.class);
		if(null!=v){
			v.setState(ATTSystem.DEV);
			v.setCreateTime(new Date());
			articleService.updateObject(v);
		}
		return SUCCESS;
   }
	
	
   public String uploadFile(){
	   String callback = HttpHelp.getRequest().getParameter("CKEditorFuncNum");
	   String path = FolderManager.uploadFile(upload,uploadFileName);
	   setAttr("callback", callback);
	   setAttr("path", path);
	   return SUCCESS;
   }
   public String searchArticle(){
	   Page page = PagerFactory.createPage();
	   articleService.searchArticle(page, article);
	   setAttr(ATTPage.PAGE_LIST, page);
	   return SUCCESS;
   }
   public String toArticle(){
	   article = articleService.getObject(id, Article.class);
	   List<Subject> list = articleService.querySubject();
	   super.setAttr("sub_list", articleService.querySubject());
	   setAttr("article", article);
	   return SUCCESS;
   }
   public String suArticle(){
	   Article ale = articleService.getObject(id, Article.class);
	   if(ale==null){
		   //articleService.saveArticle(article, ico, icoFileName);
		   articleService.saveArticle(article, image1, image2, image3, image1FileName, image2FileName, image3FileName);
	   }else{
		   //articleService.updateArticle(ale, article, ico, icoFileName);
		   articleService.updateArticle(ale, article, image1, image2, image3, image1FileName, image2FileName, image3FileName);
	   }
	   return SUCCESS;
   }
   
   public String searchArticleTemp(){
	   Page page = PagerFactory.createPage();
	   articleService.searchArticleTemp(page, null);
	   setAttr(ATTPage.PAGE_LIST, page);
	   return SUCCESS;
   }
   public String toArticleTemp(){
	   ArticleTemp articleTemp = articleService.getObject(id, ArticleTemp.class);
	   
	   List<Subject> list = articleService.querySubject();
	   super.setAttr("sub_list", articleService.querySubject());
	   setAttr("article", articleTemp);
	   return SUCCESS;
   }
   public String suArticleTemp(){
	   ArticleTemp ale = articleService.getObject(id, ArticleTemp.class);
	   if(ale!=null){
		   articleService.saveArticle(article,ale, image1, image2, image3, image1FileName, image2FileName, image3FileName);
	       ale.setState(ATTSystem.DONE);
	       ale.setCreateTime(new Date());
	       articleService.updateObject(ale);
	   }
	   return SUCCESS;
   }
   
   
   
   public String searchSubject(){
	   Page page = PagerFactory.createPage();
	   articleService.searchSubject(page, subject);
	   setAttr(ATTPage.PAGE_LIST, page);
	   return SUCCESS;
   }
   public String suSubject(){
	   Subject sub = articleService.getObject(id, Subject.class);
	   if(sub==null){
		    articleService.saveSubject(subject, ico, icoFileName);
	   }else{
		    articleService.updateSubject(subject, sub, ico, icoFileName);  
	   }
	   return SUCCESS;
   }
   public String toSubject(){
	   Subject sub = articleService.getObject(id, Subject.class);
	  
	   setAttr("subject", sub);
	   return SUCCESS;
   }
   
	public Article getArticle() {
		return article;
	}
	public void setArticle(Article article) {
		this.article = article;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public ArticleService getArticleService() {
		return articleService;
	}
	public void setArticleService(ArticleService articleService) {
		this.articleService = articleService;
	}
	public File getIco() {
		return ico;
	}
	public void setIco(File ico) {
		this.ico = ico;
	}
	public String getIcoFileName() {
		return icoFileName;
	}
	public void setIcoFileName(String icoFileName) {
		this.icoFileName = icoFileName;
	}
	public File getImage1() {
		return image1;
	}
	public void setImage1(File image1) {
		this.image1 = image1;
	}
	public File getImage2() {
		return image2;
	}
	public void setImage2(File image2) {
		this.image2 = image2;
	}
	public File getImage3() {
		return image3;
	}
	public void setImage3(File image3) {
		this.image3 = image3;
	}
	public String getImage1FileName() {
		return image1FileName;
	}
	public void setImage1FileName(String image1FileName) {
		this.image1FileName = image1FileName;
	}
	public String getImage2FileName() {
		return image2FileName;
	}
	public void setImage2FileName(String image2FileName) {
		this.image2FileName = image2FileName;
	}
	public String getImage3FileName() {
		return image3FileName;
	}
	public void setImage3FileName(String image3FileName) {
		this.image3FileName = image3FileName;
	}
	public File getUpload() {
		return upload;
	}
	public void setUpload(File upload) {
		this.upload = upload;
	}
	public String getUploadFileName() {
		return uploadFileName;
	}
	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	public Subject getSubject() {
		return subject;
	}
	public void setSubject(Subject subject) {
		this.subject = subject;
	}

	/**
	 * @return the imgUrl
	 */
	public String getImgUrl() {
		return imgUrl;
	}

	/**
	 * @param imgUrl the imgUrl to set
	 */
	public void setImgUrl(String imgUrl) {
		this.imgUrl = imgUrl;
	}
	
	
}
